package com.jiyun.controller;

import com.jiyun.entity.User;
import com.jiyun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-11-05 15:29:24
 */
@Controller
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;
    //登录
    @RequestMapping("login")
    public String login(User user, HttpServletRequest request){
        List<User> list = userService.findByUser(user);
        if (list.size()>0){
            //登录成功把用户存到session
            request.getSession().setAttribute("user",list.get(0));
            return "redirect:/show.jsp";
        }else{
            request.setAttribute("msg","用户名或者密码错误");
            return "redirect:/login.jsp";
        }
    }
    //注册
    @RequestMapping("register")
    public String register(User user){
        userService.insert(user);
        return "redirect:/login.jsp";
    }
}